﻿using System;
using System.Collections.Specialized;

namespace Crocodoc
{
    /// <summary>
    /// Provides access to the Crocodoc Session API. The Session API is used to to
    /// create sessions for specific documents that can be used to view a document
    /// using a specific session-based URL.
    /// </summary>
    public class CrocodocSession : Crocodoc
    {
        /// <summary>
        /// The Session API path relative to the base API path.
        /// </summary>
        private static string _path = "session/";

        /// <summary>
        /// Gets or sets the Session API path relative to the base API path.
        /// </summary>
        public new static string Path
        {
            get
            {
                return _path;
            }
            set
            {
                _path = value;
            }
        }

        /// <summary>
        /// Creates a session for a specific document by UUID.
        /// </summary>
        /// <param name="uuid">The uuid of the file to create a session for.</param>
        /// <returns>A unique session key for the document.</returns>
        public static string Create(string uuid)
        {
            return Create(uuid, null);
        }

        /// <summary>
        /// Create a session for a specific document by UUID that is optionally
        /// editable and can use user ID and name info from your application, can
        /// filter annotations, can grant admin permissions, can be downloadable, can
        /// be copy-protected, and can prevent changes from being persisted.
        /// </summary>
        /// <param name="uuid">The uuid of the file to create a session for.</param>
        /// <param name="parameters">A parameter list of options</param>
        /// <returns>A unique session key for the document.</returns>
        public static string Create(string uuid, CreateSessionOptions parameters)
        {
            NameValueCollection postParams = new NameValueCollection();
            postParams.Add("uuid", uuid);

            if (parameters != null)
            {
                if (parameters.IsEditable)
                {
                    postParams.Add("editable", "true");
                }
                if (parameters.User != null)
                {
                    postParams.Add("user", parameters.User.ToString());
                }
                if (!String.IsNullOrEmpty(parameters.Filter))
                {
                    postParams.Add("filter", parameters.Filter);
                }
                if (parameters.IsAdmin)
                {
                    postParams.Add("admin", "true");
                }
                if (parameters.IsDownloadable)
                {
                    postParams.Add("downloadable", "true");
                }
                if (parameters.IsCopyProtected)
                {
                    postParams.Add("copyprotected", "true");
                }
                if (parameters.IsDemo)
                {
                    postParams.Add("demo", "true");
                }
                if (parameters.Sidebar != SidebarOptions.None)
                {
                    postParams.Add("sidebar", parameters.Sidebar.ToString().ToLower());
                }
            }
            
            SessionResult result = PostRequest<SessionResult>(Path, "create", postParams);

            return result.Session;
        }
    }
}
